package com.wangyukai.jmonitor.ui;

import com.wangyukai.jmonitor.Config;
import com.wangyukai.jmonitor.plugin.DeployPlugin;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.lang.reflect.InvocationTargetException;

public class HomeFrame extends JFrame {

    private Logger logger = LoggerFactory.getLogger(HomeFrame.class);
    private Thread thread = null;
    public HomeFrame(){
        this.setLayout(null);
        this.setSize(800, 800);
        this.setTitle("部署工具");

        final JTextArea log_txtarea = new JTextArea();
        log_txtarea.setBounds(30, 240, 700, 510);
        log_txtarea.setFont(new Font("宋体",1,20));
        this.add(log_txtarea);

        JLabel lbl_backfilename = new JLabel();
        lbl_backfilename.setText("备份文件名:");
        lbl_backfilename.setFont(new Font("宋体",1,20));
        lbl_backfilename.setBounds(30, 100, 140, 40);

        JTextField txt_backfilename = new JTextField();
        txt_backfilename.setFont(new Font("宋体",1,20));
        txt_backfilename.setBounds(180, 100, 550, 40);

        this.add(lbl_backfilename);
        this.add(txt_backfilename);

        JLabel lbl_deployfilename = new JLabel();
        lbl_deployfilename.setText("发布文件名:");
        lbl_deployfilename.setFont(new Font("宋体",1,20));
        lbl_deployfilename.setBounds(30, 170, 140, 40);

        JTextField txt_deployfilename = new JTextField();
        txt_deployfilename.setFont(new Font("宋体",1,20));
        txt_deployfilename.setBounds(180, 170, 415, 40);
        txt_deployfilename.setEditable(false);

        JButton btn_deployfilename = new JButton("浏览");
        btn_deployfilename.setFont(new Font("宋体",1,20));
        btn_deployfilename.setBounds(605, 170, 120, 40);

        btn_deployfilename.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                int option = fileChooser.showOpenDialog(HomeFrame.this);
                if (option == JFileChooser.APPROVE_OPTION) {
                    File file = fileChooser.getSelectedFile();
                    txt_deployfilename.setText(file.getAbsolutePath());
                }
            }
        });

        this.add(lbl_deployfilename);
        this.add(txt_deployfilename);
        this.add(btn_deployfilename);

        JButton start_btn = new JButton();
        start_btn.setText("开始");
        start_btn.setFont(new Font("宋体",1,20));
        start_btn.setBounds(30, 30, 120, 40);
        start_btn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(StringUtils.isEmpty(txt_backfilename.getText())
                        || !txt_backfilename.getText().endsWith(".jar")){
                    JOptionPane.showMessageDialog(null,
                            "备份文件名必须输入!", "错误信息", JOptionPane.YES_OPTION);
                    return;
                }

                if(StringUtils.isEmpty(txt_deployfilename.getText())
                        || !txt_deployfilename.getText().endsWith(".jar")){
                    JOptionPane.showMessageDialog(null,
                            "发布名必须输入!", "错误信息", JOptionPane.YES_OPTION);
                    return;
                }


                File file = new File(txt_deployfilename.getText());

                start_btn.setEnabled(false);
                if(thread == null || thread.isInterrupted()) {
                    Config.serverinfo.stream().forEach( server -> {

                        thread = new Thread(new Runnable() {
                            @Override
                            public void run() {

                                DeployPlugin deployPlugin = new DeployPlugin( server.getIp(),
                                        server.getUser(), server.getPassword(), server.getPort(), log_txtarea);
                                deployPlugin.executeCommand(server.getServerport(), file.getName(),
                                        file.getParent(), server.getDeploypath(), txt_backfilename.getText());
                                try {
                                    SwingUtilities.invokeAndWait(() -> {
                                        start_btn.setEnabled(true);
                                    });
                                } catch (InterruptedException interruptedException) {
                                    logger.error("按钮设置错误", interruptedException);
                                } catch (InvocationTargetException invocationTargetException) {
                                    logger.error("按钮设置错误", invocationTargetException);
                                }
                            }
                        });
                        thread.start();
                    });

                }
            }
        });
        this.add(start_btn);

        JButton end_btn = new JButton();
        end_btn.setText("结束");
        end_btn.setFont(new Font("宋体",1,20));
        end_btn.setBounds(200, 30, 120, 40);
        end_btn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(thread != null && !thread.isInterrupted()) {
                    thread.interrupt();
                }
                thread = null;
                start_btn.setEnabled(true);
            }
        });
        this.add(end_btn);

        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

}
